package com.lebang.controller.sys;

import com.lebang.aop.ConfigRecord;
import com.lebang.common.Login;
import com.lebang.component.RedisClient;
import com.lebang.entity.sys.AppMain;
import com.lebang.model.Whether;
import com.lebang.service.AppMainService;
import com.lebang.service.sys.AppLookCategoryService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author: hwq
 * @Date: 2019/5/20 9:54
 * @Version 1.0
 */
@Controller
@RequestMapping("/app/config/redis")
public class AppConfigRedis {
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private AppMainService appMainService;
    @Autowired
    private AppLookCategoryService appLookCategoryService;


    @Login
    @GetMapping("/setqq")
    public String setqq(Model model) {
        String qq = redisClient.hget("api:sys:config:", "qq");
        model.addAttribute("qq", qq);
        return "/apps/qq-edit";
    }
    @Login
    @PostMapping("/setqq")
    @ResponseBody
    public JsonData setqq(String qq) {
        if (qq != null) {
            redisClient.hset("api:sys:config:", "qq", qq);
            return Callback.success("200", "设置成功", null);
        } else {
            return Callback.fail("200", "设置失败", null);
        }

    }
    @Login
    @GetMapping("/app")
    public String app() {
        return "/apps/newapi";
    }
    @Login
    @GetMapping("/app/edit")
    public String appEdit(Model model, AppMain vo) {
        if (vo.getId() != null) {
            vo = appMainService.selectById(vo.getId());
        } else {
            vo.setEnable(Whether.NO.getDbIndex());
            vo.setParentId(2);
            vo.setType(1);
            vo.setMyType(0);
        }
        model.addAttribute("item", vo);
        model.addAttribute("looklist", appLookCategoryService.getAll());
        return "/apps/newapi-edit";
    }
    @Login
    @PostMapping("/app/edit")
    @ResponseBody
    @ConfigRecord
    public JsonData appEdit(AppMain vo) {
        int i = 0;
        if (vo.getId() == null) {

            i = appMainService.insert(vo);
        } else {
            i = appMainService.update(vo);
        }
        if (i > 0) {
            redisClient.del("sys:main");
            return Callback.success("success", "操作成功", null);
        } else {
            return Callback.fail("fail", "操作失败", null);
        }
    }
    @Login
    @GetMapping("/app/add/record")
    public String record() {
        return "/apps/newapi-record";
    }

    @Login
    @PostMapping("/app/add/record")
    @ResponseBody
    public JsonData record( String taskId, String icon, String title, String type, String msg) {

        AppMain main = new AppMain();
        main.setType(2);
        if ("tb_common".equals(type)) {

            main.setType(99);
        }
        if ("tb_soso".equals(type)) {
            main.setType(98);
        }
        main.setMyType(0);
        main.setHasDeleted(Whether.NO.getDbIndex());
        main.setCreateTime(LocalDateTime.now());
        main.setModifyTime(LocalDateTime.now());
        main.setTitle(title);
        main.setPicture(icon);
        main.setSort(99);
        main.setUrl("");
        main.setMsg(msg);
        main.setEnable(Whether.YES.getDbIndex());
        main.setBrand("ALL");
        main.setParentId(2);

        main.setTask(taskId);
        int i = appMainService.insert(main);
        if (i > 0) {
            redisClient.del("sys:main");
            return Callback.success("success", "上架成功", null);
        } else {
            return Callback.fail("fail", "操作失败", null);
        }
    }
    @Login
    @ResponseBody
    @PostMapping("/get/sql")
    public JsonData sql(HttpServletRequest request,String sql) {
        return Callback.success("success", "查询成功", appMainService.getSql(sql));
    }
}
